package com.haois.mp.database.util;

import org.apache.commons.io.FileUtils;

import java.io.*;

/**
 * description
 *
 * @author RuiHaoZ on GuangZhou
 * @date 2018/9/12 11:30
 */
public class ReadFileUtil {

    public static String getFilePath(String fileName) {
        return ReadFileUtil.class.getClassLoader().getResource(fileName).getFile();
    }

    public static String readFile(String filePath) {
        String input = "";
        try {
            input = FileUtils.readFileToString(new File(filePath), "UTF-8");
        } catch (IOException e) {
            input = "ERROR:" + e.getMessage();
            e.printStackTrace();
        }
        return input;
    }

    public static String writeFile(String text, String filePath) {
        BufferedWriter writer = null;
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }
            writer = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file, false), "UTF-8"));
            writer.write(text);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }
}
